# -*- coding: utf-8 -*-
"""
Date: 2021-12-16
Author: J z
"""
from app.const.enums.activity_enums import OrderStatus
from utils.unique_key import create_idx
from framework.dal.repository import BaseRepository
from framework.dal.dal_adapters import character_adapters as cadts
from utils.time_utils import getCT


class RoleRepository(BaseRepository):
    """
    TbRole 资源库
    """
    adapter_class = cadts.RoleAdapter

    @staticmethod
    def create(mobile, password):
        """
        创建记录
        :param mobile:
        :param password:
        :return:
        """
        return cadts.RoleAdapter.objects.create(RoleGuid=create_idx(),
                                                Mobile=mobile,
                                                Password=password,
                                                CreateTime=getCT())

    @staticmethod
    def get_by_rid(rid):
        """
        根据Rid查找
        :param rid:
        :return:
        """
        return cadts.RoleAdapter.objects.get(RoleGuid=rid)

    @staticmethod
    def filter_by_rid_list(rid_list, *columns):
        """
        根据rid列表获取TbRole记录列表
        :param rid_list:
        :return:
        """
        return cadts.RoleAdapter.objects.columns(*columns).filter(RoleGuid__in=rid_list)

    @staticmethod
    def get_by_mobile(mobile):
        """
        查找手机号
        :param mobile:
        :return:
        """
        return cadts.RoleAdapter.objects.get(Mobile=mobile)


class RoleAffairRepository(BaseRepository):
    """
    TbRoleAffair 资源库
    """
    adapter_class = cadts.RoleAffairAdapter

    @staticmethod
    def create(role_guid, seat_id, service_role_guid):
        """
        创建记录
        :param role_guid:
        :param seat_id:
        :param service_role_guid:
        :return:
        """
        return cadts.RoleAffairAdapter.objects.create(AffairId=create_idx(),
                                                      RoleGuid=role_guid,
                                                      SeatId=seat_id,
                                                      ServiceRoleGuid=service_role_guid,
                                                      CreateTime=getCT())

    @staticmethod
    def get_by_affair_id(affair_id):
        """
        根据事件id获取
        :param affair_id:
        :return:
        """
        return cadts.RoleAffairAdapter.objects.get(AffairId=affair_id)


class PayRecordRepository(BaseRepository):
    """
    TbPayRecord 资源库
    """
    adapter_class = cadts.PayRecordAdapter

    @staticmethod
    def create(mobile, password):
        """
        创建记录
        :param mobile:
        :param password:
        :return:
        """
        return cadts.PayRecordAdapter.objects.create(AffairId=create_idx(),
                                                     Mobile=mobile,
                                                     Password=password,
                                                     CreateTime=getCT())

    @staticmethod
    def get_not_match_order_by_rid(rid):
        """
        根据rid获取未匹配订单
        :param rid:
        :return:
        """
        return cadts.PayRecordAdapter.objects.get(RoleGuid=rid, OrderStatus__eq=OrderStatus.NotMatch)

    @staticmethod
    def get_by_relation_affairid(affair_id):
        """
        根据关联事件(唯一)获取订单
        :param affair_id:
        :return:
        """
        return cadts.PayRecordAdapter.objects.get(RelationAffairId=affair_id)


class SeatRepository(BaseRepository):
    """
    TbSeat 资源库
    """
    adapter_class = cadts.SeatAdapter

    @staticmethod
    def get_by_service_role_guid(service_role_guid):
        """
        根据服务者rid获取记录
        :param service_role_guid:
        :return:
        """
        return cadts.SeatAdapter.objects.get(ServiceRoleGuid=service_role_guid)

    @staticmethod
    def get_by_seat_id(seat_id):
        """
        根据座位id获取记录
        :param seat_id:
        :return:
        """
        return cadts.SeatAdapter.objects.get(SeatId=seat_id)

    # @staticmethod
    # def create(mobile, password):
    #     """
    #     创建记录(后台)
    #     :param mobile:
    #     :param password:
    #     :return:
    #     """
    #     return cadts.SeatAdapter.objects.create(AffairId=create_idx(),
    #                                             Mobile=mobile,
    #                                             Password=password,
    #                                             CreateTime=getCT())

    @classmethod
    def filter_empty_seat_service_num(cls):
        """
        查询空余座位数量(有服务者)
        :return:
        """

        return len(cls.adapter_class.objects.filter(RoleGuid="", ServiceRoleGuid__ne=""))


class OrderSendRepository(BaseRepository):
    """
    TbOrderSend 资源库
    """
    adapter_class = cadts.OrderSendAdapter

    @staticmethod
    def create(role_guid, game_id, server_type, server_id, order_type, price, need_time, safe_price, quick_price,
               game_account, game_password, mobile, game_nickname, title='', game_des=''):
        """
        创建记录
        :param role_guid:
        :param game_id:
        :param server_type:
        :param server_id:
        :param order_type:
        :param title:
        :param price:
        :param need_time:
        :param safe_price:
        :param quick_price:
        :param game_account:
        :param game_password:
        :param mobile:
        :param game_nickname:
        :param game_des:
        :return:
        """
        return cadts.OrderSendAdapter.objects.create(OrderId=create_idx(),
                                                     RoleGuid=role_guid,
                                                     GameId=game_id,
                                                     ServerType=server_type,
                                                     ServerId=server_id,
                                                     OrderType=order_type,
                                                     Title=title,
                                                     Price=price,
                                                     NeedTime=need_time,
                                                     SafePrice=safe_price,
                                                     QuickPrice=quick_price,
                                                     GameAccount=game_account,
                                                     GamePassword=game_password,
                                                     GameNickName=game_nickname,
                                                     Mobile=mobile,
                                                     GameDes=game_des,
                                                     CreateTime=getCT())

    @classmethod
    def filter_order_by_column(cls, page_size, offset, order_column=""):
        """
        分页查询
        :param page_size:
        :param offset:
        :param order_column:
        :return:
        """
        if not order_column:
            order_column = "-CreateTime"
        return cls.adapter_class.objects.filter().order(order_column).limit(page_size, offset)


class ServiceRoleAffairRepository(BaseRepository):
    """
    TbServiceRoleAffair 资源库
    """
    adapter_class = cadts.ServiceRoleAffairAdapter

    @staticmethod
    def create(role_guid, lock_seat_id):
        """
        创建记录
        :param role_guid:
        :param lock_seat_id:
        :return:
        """
        return cadts.ServiceRoleAffairAdapter.objects.create(AffairId=create_idx(),
                                                             RoleGuid=role_guid,
                                                             LockSeatId=lock_seat_id,
                                                             SignTime=getCT())

    @staticmethod
    def get_by_affair_id(affair_id):
        """
        根据事件id获取
        :param affair_id:
        :return:
        """
        return cadts.ServiceRoleAffairAdapter.objects.get(AffairId=affair_id)


    @classmethod
    def filter_by_rid_order_column(cls, rid, page_size, offset, order_column=""):
        """
        分页查询
        :param rid:
        :param page_size:
        :param offset:
        :param order_column:
        :return:
        """
        if not order_column:
            order_column = "-SignTime"
        return cls.adapter_class.objects.filter(RoleGuid=rid).order(order_column).limit(page_size, offset)
